package db.manager;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import app.BaseApplication;

import com.netease.xone.dataMgr.ModelDiscuss;

import db.XoneDBTables.DraftTable;


public class ManagerDraft {
    
    public static final int ID        = 0;
    public static final int ACCOUNT   = 1;
    public static final int CONTENT   = 2;
    public static final int RECORDER  = 3;
    public static final int DURATION  = 4;
    public static final int IMAGE     = 5;
    public static final int FILTER    = 6;
    public static final int TIME      = 7;
    

    public static String[] Projection = new String[] {
        BaseColumns._ID,              // 0
        DraftTable.C_ACCOUNT,         // 1
        DraftTable.C_CONTENT,         // 2
        DraftTable.C_RECORDER,        // 3
        DraftTable.C_DURATION,        // 4
        DraftTable.C_IMAGE,           // 5
        DraftTable.C_FILTER,          // 6
        DraftTable.C_TIME             // 7
    };
    
    
    private static Context mContext = BaseApplication.getAppInstance().getApplicationContext();
    
    /**
     * 插入或更新ModelDiscuss
     * @param data
     */
    public static void save(ModelDiscuss data) {
        if (null == data) {
            return;
        }
        
        ContentValues values = null;
        String where = null;
        String[] selectionArgs = null;
        
        values = new ContentValues();
        values.put(DraftTable.C_ACCOUNT, ManagerAccount.getInstance().getCurrentAccountId());
        values.put(DraftTable.C_CONTENT, data.content);
        values.put(DraftTable.C_RECORDER, data.recorder);
        values.put(DraftTable.C_DURATION, data.duration);
        values.put(DraftTable.C_IMAGE, data.image);
        values.put(DraftTable.C_FILTER, data.filter);
        values.put(DraftTable.C_TIME, data.time);
        
        if (data.id > 0 ) {
            where = BaseColumns._ID + "=？";
            selectionArgs = new String[] {String.valueOf(data.id)};
            mContext.getContentResolver().update(DraftTable.CONTENT_URI, values, where, selectionArgs);
        } else {
            mContext.getContentResolver().insert(DraftTable.CONTENT_URI, values);
        }
    }
    
    /**
     * 根据id来恢复ModelDiscuss
     * @param data
     */
    public static void restore(ModelDiscuss data) {
        if (null == data) {
            return;
        }
        
        if (data.id < 0) {
            return;
        }
        
        String selection = null;
        String[] selectionArgs = null;
        
        selection = BaseColumns._ID + "=？";
        selectionArgs = new String[] {String.valueOf(data.id)};
        Cursor cursor = mContext.getContentResolver().query(DraftTable.CONTENT_URI, Projection, selection, selectionArgs, null);
        
        data.content = cursor.getString(CONTENT);
        data.recorder = cursor.getString(RECORDER);
        data.duration = cursor.getInt(DURATION);
        data.image = cursor.getString(IMAGE);
        data.filter = cursor.getInt(FILTER);
        data.time = cursor.getLong(TIME);
    }
}
